import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../../provider/goods_detail.dart';
import '../../provider/cart.dart';
import '../../provider/route_change.dart';
import 'package:flutter_html/flutter_html.dart';
import '../../model/cart.dart';
import 'package:fluttertoast/fluttertoast.dart';

class GoodsDetailPage extends StatelessWidget {
  final String goodsId;
  const GoodsDetailPage({Key key, this.goodsId}) : super(key: key);

  Widget _buildBaseInfo(context) {
    return Provide<GoodsDetailProvide>(
        builder: (context, child, goodsDetailProvide) {
      if (goodsDetailProvide.goodsData != null) {
        var goods = goodsDetailProvide.goodsData.goodInfo;
        return Container(
            width: ScreenUtil().setWidth(750),
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.all(10.0),
                  width: ScreenUtil().setWidth(740),
                  height: ScreenUtil().setHeight(700),
                  child: Image.network(goods.image1),
                ),
                Container(
                  width: ScreenUtil().setWidth(740),
                  margin: EdgeInsets.all(10.0),
                  child: Text(goods.goodsName),
                ),
                Container(
                  width: ScreenUtil().setWidth(740),
                  margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
                  child: Text('编号：${goods.goodsSerialNumber}',
                      style: TextStyle(
                          color: Colors.black38,
                          fontSize: ScreenUtil().setSp(24))),
                ),
                Container(
                    width: ScreenUtil().setWidth(740),
                    margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    child: Row(
                      children: <Widget>[
                        Text('￥${goods.presentPrice}',
                            style: TextStyle(
                                color: Colors.pinkAccent,
                                fontSize: ScreenUtil().setSp(40))),
                        Text(
                          '  市场价：${goods.oriPrice}',
                          style: TextStyle(
                              color: Colors.black26,
                              fontSize: ScreenUtil().setSp(24),
                              decoration: TextDecoration.lineThrough),
                        ),
                      ],
                    )),
                Container(
                  width: ScreenUtil().setWidth(740),
                  height: ScreenUtil().setHeight(80),
                  margin: EdgeInsets.all(10.0),
                  alignment: Alignment.centerLeft,
                  decoration: BoxDecoration(color: Colors.white),
                  child: Text('说明：>急速送达>正品保证',
                      style: TextStyle(
                          color: Colors.redAccent,
                          fontSize: ScreenUtil().setSp(30))),
                ),
              ],
            ));
      } else {
        return Center(child: Text('数据加载中...'));
      }
    });
  }

  Widget _buildDetailAndComment(context) {
    return Provide<GoodsDetailProvide>(
        builder: (context, child, goodsDetailProvide) {
      if (goodsDetailProvide.goodsData != null) {
        var goodsDetail = goodsDetailProvide.goodsData.goodInfo.goodsDetail;
        var currentIndex = goodsDetailProvide.currentIndex;

        return Column(
          children: <Widget>[
            Container(
              width: ScreenUtil().setWidth(750),
              height: ScreenUtil().setHeight(100),
              child: Row(
                children: <Widget>[
                  InkWell(
                      onTap: () {
                        Provide.value<GoodsDetailProvide>(context)
                            .setCurrentIndex(0);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        height: ScreenUtil().setHeight(100),
                        width: ScreenUtil().setWidth(375),
                        child: Text('详情',
                            style: TextStyle(
                                color: currentIndex == 0
                                    ? Colors.pinkAccent
                                    : Colors.black,
                                fontSize: ScreenUtil().setSp(30))),
                      )),
                  InkWell(
                      onTap: () {
                        Provide.value<GoodsDetailProvide>(context)
                            .setCurrentIndex(1);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        height: ScreenUtil().setHeight(100),
                        width: ScreenUtil().setWidth(375),
                        child: Text('评论',
                            style: TextStyle(
                                color: currentIndex == 1
                                    ? Colors.pinkAccent
                                    : Colors.black,
                                fontSize: ScreenUtil().setSp(30))),
                      ))
                ],
              ),
            ),
            Container(
              child: currentIndex == 0
                  ? Container(
                      width: ScreenUtil().setWidth(750),
                      child: Html(data: goodsDetail),
                    )
                  : Container(
                      padding: EdgeInsets.only(bottom: 100),
                      child: Center(
                        child: Text('暂无评论...'),
                      )),
            )
          ],
        );
      } else {
        return Center(
          child: Text(''),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.pop(context);
              }),
          title: Text('商品详情')),
      body: FutureBuilder(
        future: _getGoodsDetailData(context),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
              children: <Widget>[
                ListView(
                  children: <Widget>[
                    _buildBaseInfo(context),
                    _buildDetailAndComment(context)
                  ],
                ),
                Positioned(
                    bottom: 0,
                    left: 0,
                    child: Provide<GoodsDetailProvide>(
                        builder: (context, child, goodsDetailProvide) {
                      if (goodsDetailProvide.goodsData != null) {
                        var goods = goodsDetailProvide.goodsData.goodInfo;
                        return Container(
                          height: ScreenUtil().setHeight(100),
                          decoration: BoxDecoration(color: Colors.white),
                          child: Row(
                            children: <Widget>[
                              Container(
                                  width: ScreenUtil().setWidth(150),
                                  height: ScreenUtil().setHeight(100),
                                  alignment: Alignment.center,
                                  child: InkWell(
                                    onTap: () {
                                      Provide.value<RouteChangeProvide>(context)
                                          .changeRoute(2);
                                      Navigator.pop(context);
                                    },
                                    child: Stack(children: <Widget>[
                                      Container(
                                          width: ScreenUtil().setWidth(150),
                                          height: ScreenUtil().setHeight(100),
                                          child: Icon(Icons.shopping_cart,
                                              color: Color.fromRGBO(
                                                  234, 83, 65, 1.0))),
                                      Provide<CartProvide>(builder:
                                          (context, child, cartProvide) {
                                        if (cartProvide.allCount > 0) {
                                          return Positioned(
                                              right: 6,
                                              top: 3,
                                              child: Container(
                                                  decoration: BoxDecoration(
                                                    color: Colors.red,
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            10.0),
                                                  ),
                                                  alignment: Alignment.center,
                                                  width: 20,
                                                  height: 20,
                                                  child: Text(
                                                      '${cartProvide.allCount}',
                                                      style: TextStyle(
                                                          color: Colors.white,
                                                          fontSize: ScreenUtil()
                                                              .setSp(26)))));
                                        } else {
                                          return Text('');
                                        }
                                      })
                                    ]),
                                  )),
                              Container(
                                  width: ScreenUtil().setWidth(300),
                                  height: ScreenUtil().setHeight(100),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      color: Color.fromRGBO(96, 178, 91, 1.0)),
                                  child: InkWell(
                                      onTap: () {
                                        Fluttertoast.showToast(
                                            msg: "加入成功~",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.black54,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                        Provide.value<CartProvide>(context)
                                            .addToCart(Cart(
                                                goodsId: goods.goodsId,
                                                goodsName: goods.goodsName,
                                                goodsPrice: goods.presentPrice,
                                                goodsPic: goods.image1,
                                                count: 1,
                                                isSelect: true));
                                      },
                                      child: Text('加入购物车',
                                          style: TextStyle(
                                              color: Colors.white,
                                              fontSize:
                                                  ScreenUtil().setSp(30))))),
                              Container(
                                  width: ScreenUtil().setWidth(300),
                                  height: ScreenUtil().setHeight(100),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      color: Color.fromRGBO(234, 83, 65, 1.0)),
                                  child: InkWell(
                                    onTap: () {
                                      Provide.value<CartProvide>(context)
                                          .clearCart();
                                    },
                                    child: Text('立即购买',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: ScreenUtil().setSp(30))),
                                  ))
                            ],
                          ),
                        );
                      } else {
                        return Center(child: Text(''));
                      }
                    }))
              ],
            );
          } else {
            return Text('数据加载中');
          }
        },
      ),
    );
  }

  Future _getGoodsDetailData(context) async {
    await Provide.value<GoodsDetailProvide>(context)
        .getGoodsDetailDataById(goodsId);

    return '数据加载完毕';
  }
}
